<template>
  <basic-container>
    <el-container>
      <el-aside width="200px">
        <el-tree
          :props="treeOption"
          :data="treeDeptData"
          default-expand-all
          :expand-on-click-node="false"
          highlight-current
          @node-click="nodeClick"
        />
      </el-aside>
      <el-main>
        <avue-crud
          ref="crud"
          :option="tableOption"
          :page.sync="page"
          :table-loading="listLoading"
          :data="list"
          @on-load="getList"
          @search-change="searchChange"
          @search-reset="searchReset"
          @refresh-change="refreshChange"
        >
        <template slot="avatar" slot-scope="scope">
            <img width="60px" height="60px" :src="scope.row.avatar">
        </template>
          <template slot="menuLeft">
            <el-button
              class="filter-item"
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="handleAdd"
            >新增员工</el-button>
          </template>
          <template slot="menuLeft">
            <employee_import ref="employeeImport" style="display: inline" />
          </template>

          <template slot="menu" slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              icon="el-icon-edit"
              @click="go_detail(scope.row,scope.index)"
            >详情</el-button>
            <el-button
              type="text"
              size="mini"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row,scope.index)"
            >编辑</el-button>
            <el-button
              type="text"
              size="mini"
              icon="el-icon-delete"
              @click="handleDelete(scope.row,scope.index)"
            >删除</el-button>
          </template>
        </avue-crud>
      </el-main>
    </el-container>
  </basic-container>
</template>

<script>
import { delObj, fetchList } from "@/api/crm/marketemployee";
import { tableOption } from "@/const/crud/crm/team";
import employee_import from "../../components/crm/excel_import/market_employee_import";
import { fetchTree } from "@/api/admin/dept";

export default {
  name: "employee",
  data() {
    return {
      treeOption: {
        label: "name",
        value: "id",
        children: "children"
      },
      treeDeptData: [],
      tableOption: tableOption,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条,
        isAsc: false // 是否倒序
      },
      list: [],
      listLoading: true
    };
  },
  components: {
    employee_import
  },
  created() {
    this.handleDept();
  },
  computed: {
    searchForm() {
      return this.$refs.crud.searchForm;
    }
  },
    mounted() {
    this.getHeight();
  },
  methods: {
    getHeight() {
      let tableHeight = Math.max(window.innerHeight - 500, 230);
      this.tableOption.maxHeight = tableHeight + "px";
    },
    nodeClick(node) {
      this.searchForm.deptId = node.id;
      this.getList(this.page, this.filterForm(this.searchForm));
    },
    handleDept() {
      fetchTree().then(response => {
        this.treeDeptData = response.data.data;
      });
    },
    getList(page, params) {
      this.listLoading = true;
      fetchList(
        Object.assign(
          {
            current: page.currentPage,
            size: page.pageSize
          },
          params
        )
      ).then(response => {
        console.log(response);

        this.list = response.data.data.records;
        this.page.total = response.data.data.total;
        this.listLoading = false;
      });
    },
    go_detail(row) {
      let query = {
        res: {
          id: row.id
        }
      };
      this.$router.push({ path: "/team/teamDetail", query: query });
    },
    handleUpdate(row) {
      let query = {
        res: {
          id: row.id
        }
      };
      this.$router.push({ path: "/team/editTeam", query: query });
    },
    handleDelete: function(row, index) {
      let _this = this;
      this.$confirm("是否确认删除员工" + row.name, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delObj(row.id);
        })
        .then(data => {
          _this.$message.success("删除成功");
          this.getList(this.page);
        });
    },
    /**
     * 搜索回调
     */
    searchChange(form, done) {
      this.getList(this.page, form);
      done();
    },
    /**
     * 刷新回调
     */
    refreshChange() {
      this.getList(this.page, this.filterForm(this.searchForm));
    },
    searchReset() {
      this.searchForm.deptId = "";
    },
    handleAdd() {
      this.$router.push({ path: "/team/addTeam", src: "" });
    }
  }
};
</script>

